import SwiftUI
import SPGlassKit

public struct GlassWaveformExample: GlassExample {
    public static let componentID = "glass-waveform"
    /// Aggregated once, when the example is created, not on every frame.
    @State private var voice = GlassWaveformData(samples: Self.syntheticSamples(count: 48_000), maxBuckets: 96, aggregation: .rms)
    @State private var peaks = GlassWaveformData(samples: Self.syntheticSamples(count: 48_000), maxBuckets: 160)
    @State private var progress = 0.35

    public init() {}

    public var body: some View {
        VStack(alignment: .leading, spacing: 16) {
            Text("RMS, 96 buckets, with progress")
                .font(.headline)
            GlassWaveform(voice, progress: progress)
            Slider(value: $progress, in: 0...1) { Text("Progress") }

            Text("Peak, 160 buckets")
                .font(.headline)
            GlassWaveform(peaks, minimumHeight: 72)

            Text("Progress without the playhead")
                .font(.headline)
            GlassWaveform(voice, progress: progress, showsPlayhead: false)

            Text("No samples")
                .font(.headline)
            GlassWaveform(.empty, minimumHeight: 24)

            Text("Sample data · a synthetic signal built from sine waves.")
                .font(.caption)
                .foregroundStyle(.secondary)
        }
        .padding()
    }

    /// A deterministic signal: a carrier shaped by two slow envelopes.
    static func syntheticSamples(count: Int) -> [Float] {
        (0..<count).map { index in
            let t = Double(index) / Double(count)
            let envelope = 0.55 + 0.45 * sin(t * .pi * 6)
            let swell = 0.6 + 0.4 * cos(t * .pi * 17)
            return Float(sin(Double(index) * 0.21) * envelope * swell)
        }
    }
}

#Preview("GlassWaveform") { GlassWaveformExample().glassTheme(.aurora) }
